/*
 * @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *     
 *     http://www.apache.org/licenses/LICENSE-2.0  
 *     
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.
 */

package com.mcgrobot.filter;

import java.io.File;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import com.mcgrobot.common.Constants;
import com.mcgrobot.util.LevelDbUtil;

/**
 * 
 * @ClassName:   StartFilter   
 * @Description: TODO(第一启动过滤器) 
 * @author:      缪聪(mcg-helper@qq.com)
 * @date:        2018年3月9日 下午5:27:29  
 *
 */
@WebFilter
public class StartFilter implements Filter {
	
	private static Logger logger = LoggerFactory.getLogger(StartFilter.class);
	
    private FilterConfig filterConfig;

    @Value("${mcgrobot.data.path}")
    private String dataPath;
    @Value("${mcgrobot.fs.path}")
    private String fsPath;
    

    /**
     * 初始化filter
     * @param filterConfig filter的配置信息
     * @throws ServletException 如果初始化失败
     */
    @Override
	public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;

		Constants.WEB_PATH = filterConfig.getServletContext().getRealPath("/").replaceAll("\\\\", "/");
		logger.info("应用运行路径：{}", Constants.WEB_PATH);
		
    	if(StringUtils.isEmpty(fsPath)) {
    		logger.error("文件管理路径未配置，将会导致文件管理功能不能使用，请停止应用设置路径");
    	} else {
    		Constants.FS_PATH = fsPath;
    		logger.info("文件管理路径：{}", Constants.FS_PATH);
    		File mcgFile = new File(Constants.FS_PATH);
        	if (!mcgFile.exists()) {
        		mcgFile.mkdirs();
        	}
    	}		
		
    	if(StringUtils.isEmpty(dataPath)) {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.append(Constants.WEB_PATH).append("../resources/").append("data").append(File.separator);
            Constants.DATA_PATH = sBuilder.toString();
    	} else {
    		Constants.DATA_PATH =  dataPath;
    	}
	    logger.info("数据文件路径：{}", Constants.DATA_PATH);
        try {
            LevelDbUtil.init();
        } catch (IOException e) {
        	logger.error("初始化levelDb失败，异常信息：{}", e);
        }
	    
	}

	/**
     * 初始化filter。
     * @throws ServletException 如果初始化失败
     */
    public void init() throws ServletException {

    }

    public void destroy() {
        filterConfig = null;
    }

    /**
     * 取得filter的配置信息。
     * @return <code>FilterConfig</code>对象
     */
    public FilterConfig getFilterConfig() {
        return filterConfig;
    }

    /**
     * 取得servlet容器的上下文信息。
     * @return <code>ServletContext</code>对象
     */
    public ServletContext getServletContext() {
        return getFilterConfig().getServletContext();
    }

    /**
     * 执行filter.
     *
     * @param request HTTP请求
     * @param response HTTP响应
     * @param chain filter链
     *
     * @throws IOException 处理filter链时发生输入输出错误
     * @throws ServletException 处理filter链时发生的一般错误
     */    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	chain.doFilter(request, response);
    }

}